﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace IronSharePoint.Events
{
    public class IronItemEventReceiver : SPItemEventReceiver
    {
        public IronScriptEngine InitializeScriptEngine(string receiverData)
        {
            Guid featureId = Guid.Empty;
            string scriptPath = null;

            if (receiverData.Contains(","))
            {
                string[] splitArr = receiverData.Split(',');
                scriptPath = splitArr[0];
                featureId = new Guid(splitArr[1]);

            }
            else
                scriptPath = receiverData;

            IronScriptEngine engine = new IronScriptEngine();
            engine.ScriptResolver.FeatureId = featureId;
            engine.CompileScriptFrom(scriptPath);

            return engine;
        }

        public override void ContextEvent(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ContextEvent"))
            {
                object method = engine.Scope.GetVariable("ContextEvent");
                engine.Operations.Call(method, properties);
            }
            else
                base.ContextEvent(properties);
        }

        public override void ItemAdded(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemAdded"))
            {
                object method = engine.Scope.GetVariable("ItemAdded");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemAdded(properties);
        }

        public override void ItemAdding(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemAdding"))
            {
                object method = engine.Scope.GetVariable("ItemAdding");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemAdding(properties);
        }

        public override void ItemAttachmentAdded(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemAttachmentAdded"))
            {
                object method = engine.Scope.GetVariable("ItemAttachmentAdded");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemAttachmentAdded(properties);
        }

        public override void ItemAttachmentAdding(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemAttachmentAdding"))
            {
                object method = engine.Scope.GetVariable("ItemAttachmentAdding");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemAttachmentAdding(properties);
        }

        public override void ItemAttachmentDeleted(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemAttachmentDeleted"))
            {
                object method = engine.Scope.GetVariable("ItemAttachmentDeleted");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemAttachmentDeleted(properties);
        }

        public override void ItemAttachmentDeleting(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemAttachmentDeleting"))
            {
                object method = engine.Scope.GetVariable("ItemAttachmentDeleting");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemAttachmentDeleting(properties);
        }

        public override void ItemCheckedIn(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemCheckedIn"))
            {
                object method = engine.Scope.GetVariable("ItemCheckedIn");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemCheckedIn(properties);
        }

        public override void ItemCheckedOut(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemCheckedOut"))
            {
                object method = engine.Scope.GetVariable("ItemCheckedOut");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemCheckedOut(properties);
        }

        public override void ItemCheckingIn(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemCheckingIn"))
            {
                object method = engine.Scope.GetVariable("ItemCheckingIn");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemCheckingIn(properties);
        }

        public override void ItemCheckingOut(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemCheckingOut"))
            {
                object method = engine.Scope.GetVariable("ItemCheckingOut");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemCheckingOut(properties);
        }

        public override void ItemDeleted(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemDeleted"))
            {
                object method = engine.Scope.GetVariable("ItemDeleted");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemDeleted(properties);
        }

        public override void ItemDeleting(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemDeleting"))
            {
                object method = engine.Scope.GetVariable("ItemDeleting");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemDeleting(properties);
        }

        public override void ItemFileConverted(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemFileConverted"))
            {
                object method = engine.Scope.GetVariable("ItemFileConverted");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemFileConverted(properties);
        }

        public override void ItemFileMoved(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemFileMoved"))
            {
                object method = engine.Scope.GetVariable("ItemFileMoved");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemFileMoved(properties);
        }

        public override void ItemFileMoving(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemFileMoving"))
            {
                object method = engine.Scope.GetVariable("ItemFileMoving");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemFileMoving(properties);
        }

        public override void ItemUncheckedOut(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemUncheckedOut"))
            {
                object method = engine.Scope.GetVariable("ItemUncheckedOut");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemUncheckedOut(properties);
        }

        public override void ItemUncheckingOut(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemUncheckingOut"))
            {
                object method = engine.Scope.GetVariable("ItemUncheckingOut");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemUncheckingOut(properties);
        }

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemUpdated"))
            {
                object method = engine.Scope.GetVariable("ItemUpdated");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemUpdated(properties);
        }

        public override void ItemUpdating(SPItemEventProperties properties)
        {
            IronScriptEngine engine = InitializeScriptEngine(properties.ReceiverData);

            if (engine.Scope.ContainsVariable("ItemUpdating"))
            {
                object method = engine.Scope.GetVariable("ItemUpdating");
                engine.Operations.Call(method, properties);
            }
            else
                base.ItemUpdating(properties);
        }
    }
}
